package com.example.autopia.view.Adapters

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.autopia.databinding.ViewholderColorItemBinding
import com.example.autopia.databinding.ViewholderNewsHotnewsImageBinding
import com.example.autopia.model.Item.Car
import com.example.autopia.model.Item.HotNews
import com.example.autopia.view.Listeners.CarItemListener
import com.example.autopia.view.Listeners.ColorItemListener
import com.example.autopia.view.Listeners.MyCarsListener

class CarItemAdapter :
    RecyclerView.Adapter<CarItemAdapter.ViewHolder>() {

    private lateinit var list : MutableList<Car>
    private lateinit var listener: CarItemListener

    fun attachListener(listener: CarItemListener) {
        this.listener = listener
    }

    fun setData(newList : List<Car>){
        list = newList.toMutableList()
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ViewholderColorItemBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val cars = list[position]
        holder.bind(cars,listener)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    inner class ViewHolder(private val binding: ViewholderColorItemBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(car : Car,listener: CarItemListener) {
            binding.tvColor.text = car.model + " - " + car.number
            binding.btColor.setOnClickListener {
                listener.onCarItemClicked(car)
            }
        }
    }
}